package com.aiwhose.aiwhosewechataiwhose.service.impl;

import com.aiwhose.aiwhosecore.bean.Result;
import com.aiwhose.aiwhosecore.bean.security.User;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.aiwhose.aiwhosewechataiwhose.mapper.UserMapper;
import com.aiwhose.aiwhosewechataiwhose.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户表 服务层实现。
 *
 * @author 田国刚
 * @since 1.0.1
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    UserMapper userMapper;
    @Override
    public List<User> getUserByWXToken(String openToken) {
        QueryWrapper userQueryWrapper = new QueryWrapper();
        userQueryWrapper.eq(User::getWxOpenId, openToken);
        List<User> list = userMapper.selectListByQuery(userQueryWrapper);

        return list;
    }

    @Override
    public Result saveUser(User user) {
        QueryWrapper userQueryWrapper = new QueryWrapper();

        userQueryWrapper.eq(User::getWxOpenId, user.getWxOpenId());
        long integer = userMapper.selectCountByQuery(userQueryWrapper);
        if (integer > 0) {
            return Result.error(200, "用户已注册，请直接登录");
        }
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return Result.ok("注册成功");
        } else {
            return Result.error("注册失败");
        }
    }
}
